package phase3;

import java.util.*;
import java.io.BufferedInputStream;
import java.io.DataInputStream;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;

import java.io.IOException;
import java.net.URL;

public class Dictionary {

	// private static final char A = 0;
	private char index;
	private Vector<String> dictionaryWords;
	private int limit;

	// default constructor
	public Dictionary() {
		index = 0;
		dictionaryWords = new Vector<String>();
		limit = 0;
	}

	public Dictionary(char index){
		setIndex(index);
		limit=0;
		dictionaryWords = getAll();
	}
	
	// copy constructor
	public void setIndex(char index) {
		if(index >= 'a' && index <= 'z')
			this.index = index;
		else
			this.index = 0;
		
		dictionaryWords = getAll();
	}
	
	public char getIndex(){
		return this.index;
	}
	
	public int getNumOfWords(){
		return dictionaryWords.size();
	}
	
	// get all the words from a certain text file
	public Vector<String> getAll() {
		Vector<String> tempVector = new Vector<String>();
		File file = new File("wordsDB/" + index + ".txt");
		FileInputStream fis = null;
		BufferedInputStream bis = null;
		DataInputStream dis = null;

		BufferedReader reader = null;

		try {
			reader = new BufferedReader(new FileReader(file));
			String temp = null;
			while ((temp = reader.readLine()) != null) {
				tempVector.add(temp);
			}
			reader.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return tempVector;
	}

	// find the maximum length in three lists starting by three characters in
	// the first block
	public int getMaxLength(char a, char b, char c) {

		Vector<String> tempDictionaryWords = new Vector<String>();

		if (a != ' ') {
			setIndex(a);
			tempDictionaryWords.addAll(getAll());
		}
		if (b != ' ') {
			setIndex(b);
			tempDictionaryWords.addAll(getAll());
		}
		if (c != ' ') {
			setIndex(c);
			tempDictionaryWords.addAll(getAll());
		}

		for (int i = 0; i < tempDictionaryWords.size(); i++) {
			if (tempDictionaryWords.get(i).length() > limit)
				limit = tempDictionaryWords.get(i).length();
		}
		return limit;
	}

	public boolean check(String word) {
		for(int i=0;i<dictionaryWords.size();i++){
			if(dictionaryWords.get(i).equalsIgnoreCase(word)){
				return true;
			}
		}
		return false;
	}
}
